package com.example.bootdemo.queue1;


import java.util.Scanner;

public class ArrayQueueDemo1 {
    public static void main(String[] args) {
        //测试一把
        //创建一个队列
       ArrayQueue queue = new ArrayQueue(3);
        char key = ' '; //接收用户输入
        Scanner scanner = new Scanner(System.in);//
        boolean loop = true;
        //输出一个菜单
        while(loop) {
            System.out.println("s(show): 显示队列");
            System.out.println("e(exit): 退出程序");
            System.out.println("a(add): 添加数据到队列");
            System.out.println("g(get): 从队列取出数据");
            System.out.println("h(head): 查看队列头的数据");
            key = scanner.next().charAt(0);//接收一个字符
            switch (key) {
                case 's':
                    queue.showQueue();
                    break;
                case 'a':
                    System.out.println("输出一个数");
                    int value = scanner.nextInt();
                    queue.addQueue(value);
                    break;
                case 'g': //取出数据
                    try {
                        int res = queue.getQueue();
                        System.out.printf("取出的数据是%d\n", res);
                    } catch (Exception e) {
                        // TODO: handle exception
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h': //查看队列头的数据
                    try {
                        int res = queue.headQueue();
                        System.out.printf("队列头的数据是%d\n", res);
                    } catch (Exception e) {
                        // TODO: handle exception
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e': //退出
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }

        System.out.println("程序退出~~");
    }
}

    class ArrayQueue{
        private final int maxSize; // 表示数组的最大容量
        private int front; // 队列头
        private int rear; // 队列尾
        private final int[] arr; // 该数据用于存放数据, 模拟队列
        public  ArrayQueue (int maxSize){
            this.maxSize=maxSize;
            this.front=-1;
            this.rear=-1;
            this.arr=new int[maxSize];
        }
        public boolean  isFull(){
            return this.rear==this.maxSize-1;
        }
        public void addQueue(int a){
            if (isFull()){
                throw new RuntimeException("队列已满");

            }
             this.rear++;
             this.arr[this.rear]=a;
        }
        public int getQueue() {
            if (isEmpty()){
                throw new RuntimeException("队列为空");
            }
            this.front++;
            int a=this.arr[this.front];
            this.arr[this.front]=0;
            return a;
        }
        // 显示队列的所有数据
        public void showQueue() {
            // 遍历
            if (isEmpty()) {
                System.out.println("队列空的，没有数据~~");
                return;
            }
            for (int i = 0; i <this.arr.length ; i++) {
                System.out.println(this.arr[i]);
            }
        }
        // 显示队列的头数据， 注意不是取出数据
        public int headQueue() {
            return this.arr[this.front];
        }

        public boolean isEmpty() {

            return this.rear==this.front;
        }

}
